package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.pojo.Doctor;
import cn.edu.neusoft.demo.hospital2.Service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    // 获取所有医生列表
    @GetMapping("/all")
    public Map<String, Object> getAllDoctors() {
        Map<String, Object> result = new HashMap<>();
        List<Doctor> doctors = doctorService.getAllDoctors();
        result.put("code", 200);
        result.put("data", doctors);
        result.put("message", "查询成功");
        return result;
    }

    // 根据ID获取医生详情
    @GetMapping("/{docId}")
    public Map<String, Object> getDoctorById(@PathVariable String docId) {
        Map<String, Object> result = new HashMap<>();
        Doctor doctor = doctorService.getDoctorById(docId);

        if (doctor != null) {
            result.put("code", 200);
            result.put("data", doctor);
            result.put("message", "查询成功");
        } else {
            result.put("code", 404);
            result.put("message", "医生不存在");
        }

        return result;
    }

    // 新增医生
    @PostMapping("/add")
    public Map<String, Object> addDoctor(@RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();
        int rows = doctorService.addDoctor(doctor);
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "新增成功");
        } else {
            result.put("code", 500);
            result.put("message", "新增失败");
        }
        return result;
    }

    // 根据ID删除医生
    @DeleteMapping("/delete/{docId}")
    public Map<String, Object> deleteDoctorById(@PathVariable String docId) {
        Map<String, Object> result = new HashMap<>();
        int rows = doctorService.deleteDoctorById(docId);
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }

    // 根据ID更新医生信息
    @PutMapping("/update")
    public Map<String, Object> updateDoctorById(@RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();
        int rows = doctorService.updateDoctorById(doctor);
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "更新失败");
        }
        return result;
    }
}